//
//  KLShopPresentViewController.m
//  KunLun
//
//  Created by Mac on 2017/11/29.
//  Copyright © 2017年 stoneobs@icloud.com. All rights reserved.
//

#import "KLShopPresentViewController.h"

@interface KLShopPresentViewController ()<UIPickerViewDelegate,UIPickerViewDataSource>
@property(nonatomic, strong) STButton                     *clearButton;
@property(nonatomic, strong) UIView                     *whiteView;
@property(nonatomic, strong) UIPickerView                     *pickerView;
@property(nonatomic, strong) KLShopTypeModel                     *selectedModel;
@property(nonatomic, copy) void(^confimHandle)(KLShopTypeModel* model) ;
@end

@implementation KLShopPresentViewController

- (instancetype)initWithHandle:(void (^)(KLShopTypeModel *))handle
{
    self = [super init];
    if (self) {
        self.modalPresentationStyle = UIModalPresentationOverFullScreen;
        self.confimHandle = handle;
    }
    return self;
}
#pragma mark --vc 生命周期
- (void)viewDidLoad{
    [super viewDidLoad];
    [self configSubView];
    [self.pickerView selectRow:2 inComponent:0 animated:YES];
    if (self.array.count > 3) {
       self.selectedModel = self.array[2];
    }else{
        self.selectedModel = self.array.lastObject;
    }
    
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
}
- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    [self show];
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
}
- (void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
}
#pragma mark --subView
- (void)configSubView{
    __weak typeof(self) weakSelf =  self;
    self.clearButton = [[STButton alloc] initWithFrame:UIScreenFrame
                                                     title:nil
                                                titleColor:nil
                                                 titleFont:0
                                              cornerRadius:0
                                           backgroundColor:[[UIColor blackColor] colorWithAlphaComponent:0.4]
                                           backgroundImage:nil
                                                     image:nil];
    [weakSelf.clearButton setClicAction:^(UIButton *sender) {
        [weakSelf dismiss];
    }];
    [self.view addSubview:_clearButton];
    
    UIView * whiteView = [[UIView alloc] initWithFrame:CGRectMake(52, 0, UIScreenWidth - 104, 300)];
    whiteView.backgroundColor = [UIColor whiteColor];
    whiteView.layer.cornerRadius = 10;
    whiteView.clipsToBounds = YES;
    whiteView.centerY = UIScreenHeight / 2;
    
    UIPickerView * picker = [[UIPickerView alloc] initWithFrame:CGRectMake(15, 15, whiteView.width - 30, 200)];
    picker.backgroundColor = [UIColor whiteColor];
    picker.delegate = self;
    picker.dataSource = self;
    [whiteView addSubview:picker];
    
    STButton * comfimButton = [[STButton alloc] initWithFrame:CGRectMake(15, picker.bottom +10, picker.width, 40)
                                                        title:@"确定"
                                                   titleColor:[UIColor whiteColor]
                                                    titleFont:15
                                                 cornerRadius:8
                                              backgroundColor:KL_BlueBackGroundColor
                                              backgroundImage:nil
                                                        image:nil];
    [comfimButton setClicAction:^(UIButton *sender) {
        [weakSelf onSelectedConfimButton];
    }];
    comfimButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    [whiteView addSubview:comfimButton];
    

    
    [self.clearButton addSubview:whiteView];
    _whiteView = whiteView;
    _whiteView.hidden = NO;
    _pickerView = picker;
}
#pragma mark --Action Method
- (void)onSelectedConfimButton{
    [self dismissViewControllerAnimated:NO completion:^{
        if (self.confimHandle) {
            self.confimHandle(self.selectedModel);
        }
    }];

}
- (void)dismiss{
    [self dismissViewControllerAnimated:NO completion:nil];
}

- (void)show{
    __weak typeof(self) weakSelf =  self;
    STButton * closeButton = [[STButton alloc] initWithFrame:CGRectMake(0, _whiteView.bottom + 30, 44, 44)
                                                       title:@""
                                                  titleColor:nil
                                                   titleFont:0
                                                cornerRadius:0
                                             backgroundColor:nil
                                             backgroundImage:nil
                                                       image:[UIImage imageNamed:@"关闭"]];
    [closeButton setClicAction:^(UIButton *sender) {
        [weakSelf dismiss];
    }];
    closeButton.centerX = UIScreenWidth / 2;
    closeButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    [self.clearButton addSubview:closeButton];
    
    _whiteView.hidden = NO;
    [_whiteView st_showAnimationWithType:STAnimationTypekCATransitionFade];
}
#pragma mark --UIPickerViewDataSource
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    return self.array.count;
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    NSMutableArray * array = [NSMutableArray new];
    for (KLShopTypeModel *  model in self.array) {
        [array addObject:model.classify_id];
    }
    return array[row];
}
#pragma mark --UIPickerViewDataSource
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{

    KLShopTypeModel * model = self.array[row];
    self.selectedModel = model;
}
@end
